package com.ruoyi.project.module.wtc;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgInformationOrdering;
import com.ruoyi.project.service.IYqgInformationOrderingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 点餐信息暂存Controller
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/wtc/ordering")
public class WtcYqgInformationOrderingController extends BaseController {
    @Autowired
    private IYqgInformationOrderingService yqgInformationOrderingService;

    /**
     * 查询点餐信息暂存列表
     */
    //@RequiresPermissions("project:ordering:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgInformationOrdering yqgInformationOrdering) {
        startPage();
        List<YqgInformationOrdering> list = yqgInformationOrderingService.selectYqgInformationOrderingListNomal(yqgInformationOrdering);
        return getDataTable(list);
    }

    /**
     * 导出点餐信息暂存列表
     */
    //@RequiresPermissions("project:ordering:export")
    @Log(title = "点餐信息暂存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgInformationOrdering yqgInformationOrdering) {
        List<YqgInformationOrdering> list = yqgInformationOrderingService.selectYqgInformationOrderingList(yqgInformationOrdering);
        ExcelUtil<YqgInformationOrdering> util = new ExcelUtil<YqgInformationOrdering>(YqgInformationOrdering.class);
        util.exportExcel(response, list, "点餐信息暂存数据");
    }

    /**
     * 获取点餐信息暂存详细信息
     */
    //@RequiresPermissions("project:ordering:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(yqgInformationOrderingService.selectYqgInformationOrderingByID(ID));
    }

    /**
     * 新增点餐信息暂存
     */
    //@RequiresPermissions("project:ordering:add")
    @Log(title = "新增点餐信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgInformationOrdering yqgInformationOrdering) {
        return toAjax(yqgInformationOrderingService.insertYqgInformationOrdering(yqgInformationOrdering));
    }

    /**
     * 修改点餐信息暂存
     */
    //@RequiresPermissions("project:ordering:edit")
    @Log(title = "点餐信息暂存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgInformationOrdering yqgInformationOrdering) {
        return toAjax(yqgInformationOrderingService.updateYqgInformationOrdering(yqgInformationOrdering));
    }

    /**
     * 删除点餐信息暂存
     */
    //@RequiresPermissions("project:ordering:remove")
    @Log(title = "点餐信息暂存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(yqgInformationOrderingService.deleteYqgInformationOrderingByIDs(IDs));
    }

    //生成订单号
    @GetMapping(value = "/orderId")
    public Long orderId() {
        List<YqgInformationOrdering> list = yqgInformationOrderingService.selectYqgInformationOrderingList(new YqgInformationOrdering());
        if (list.size() == 0) {
            return 1L;
        } else {
            String id = list.get(0).getOrderNumber();
            id = id.substring(id.length() - 3);
            Long orderId = Long.parseLong(id) + 1;
            //String oId = orderId.toString();
            return orderId;
        }
    }
}
